//
//  ViewController.m
//  details(个人详情页纯代码)
//
//  Created by 李松 on 2017/11/9.
//  Copyright © 2017年 李松. All rights reserved.
//

#import "ViewController.h"
#import "UINavigationController+alpha.h"
#import "DetailsViewController.h"

#define WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define ORIH 200
#define OFS -260
#define NAVH  ([[UIScreen mainScreen] bounds].size.height == 812.0f ? 88.0f : 64.0f)


@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(strong, nonatomic)UIView *headView;
@property(strong, nonatomic)UIImageView *imageView;
@property(strong, nonatomic)UIView *centerView;
@property(strong, nonatomic)UITableView *tableView;

@end

static NSString *ID = @"cell";
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.headView];
    [self.view addSubview:self.centerView];
    [self.headView addSubview:self.imageView];
    
    
    self.tableView.contentInset = UIEdgeInsetsMake(260, 0, 0, 0);
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNeedsNavigationBackground:0];
}

- (UIView *)headView{
    
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, ORIH)];
        _headView.backgroundColor = [UIColor orangeColor];
        _headView.clipsToBounds = YES;
    }
    return _headView;
}

- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.headView.frame.size.height)];
        _imageView.backgroundColor = [UIColor grayColor];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.image = [UIImage imageNamed:@"che.jpg"];
    }
    return _imageView;
}

- (UIView *)centerView{
    if (!_centerView) {
        _centerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.headView.frame.size.height, WIDTH, 60)];
        _centerView.backgroundColor = [UIColor blueColor];
    }
    return _centerView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    }
    return _tableView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat ofset = scrollView.contentOffset.y - OFS;
    CGFloat h = ORIH - ofset;
    
    if (h < NAVH) {
        h = NAVH;
    }
    
    self.headView.frame = CGRectMake(0, 0, WIDTH, h);
    self.imageView.frame = CGRectMake(0, 0, WIDTH, h);
    self.centerView.frame = CGRectMake(0, h, WIDTH, 60);
    
    CGFloat alpha = ofset / (ORIH - NAVH);
    
    [self.navigationController setNeedsNavigationBackground:alpha];
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld个", indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailsViewController *vc = [[DetailsViewController alloc] init];
    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:vc];
    
    [self presentViewController:na animated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
